package com.ws.shop.action.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.entity.ArticleCategories;
import com.ws.shop.service.ArticleCategoriesService;
import com.ws.shop.util.RequestUtil;

@Controller
@RequestMapping(value="/admin/articlecategories/*")
public class ArticleCategoriesAction extends BaseAdminAction{
	private static Logger log = Logger.getLogger(ArticleCategoriesAction.class);
	@Autowired
	private ArticleCategoriesService articleCategoriesService;

	@RequestMapping(value="index.jhtml",method=RequestMethod.GET)
	public String index(HttpServletRequest request,Model model){
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		String q_name = RequestUtil.getStringParameterByGet(request, "q_name", null);
		Page page = articleCategoriesService.findArticleCategoriesListByPage(pageno,0,q_name);
		model.addAttribute(Page.PAGE, page);
		model.addAttribute("q_name", q_name);
		return "article_categories_index";
	}
	
	@RequestMapping(value="input.jhtml",method=RequestMethod.GET)
	public String input(HttpServletRequest request,Model model){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		if(id > 0){
			ArticleCategories articleCategories = articleCategoriesService.findArticleCategoriesById(id);
			if(articleCategories == null){
				model.addAttribute(ERROR_MSG, "您要编辑的文章分类不存在");
				return ERROR;
			}
			model.addAttribute("articleCategories", articleCategories);
		}
		return "article_categories_input";
	}
	
	@RequestMapping(value="save.jhtml",method=RequestMethod.POST)
	public String save(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		String name = RequestUtil.getStringParameter(request, "name", "");
		String path = RequestUtil.getStringParameter(request, "path", "");
		int single = RequestUtil.getIntParameter(request, "single", 0);
		int fid = RequestUtil.getIntParameter(request, "fid", 0);
		int ord = RequestUtil.getIntParameter(request, "ord", 0);
		ArticleCategories articleCategories = new ArticleCategories(id,name,
				path+id+ArticleCategories.PATH_SPACTOR,single,fid,ord,null);
		try{
			articleCategoriesService.saveOrUpdate(articleCategories);
			return this.ajaxJsonSuccessMessage(response, "操作成功");
		}catch(Exception e){
			log.error("操作失败："+articleCategories,e);
			return this.ajaxJsonErrorMessage(response, "操作失败");
		}
	}
	
}
